﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class OnlineBooking : DevExpress.XtraEditors.XtraForm
    {
        public OnlineBooking()
        {
            InitializeComponent();
        }

        private QueryOnlineBookingModel onlineBookingModel = null;

        private void OnlineBooking_Load(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            this.dteStartTime.DateTime = now.Date;
            this.dteEndTime.DateTime = now.Date.AddDays(1).AddSeconds(-1);
        }
        private bool ValidityCheck()
        {
            this.errorProvider1.ClearErrors();
            string message = string.Empty;
            if (this.chkQueryName.Checked)
            {
                if (Global.Instance.MandatoryFieldEmpty(this.txtCustomer, false, ref message))
                {
                    this.errorProvider1.SetError(this.txtCustomer, message);
                    this.txtCustomer.Focus();
                    return (false);
                }
            }
            if (this.chkQueryMobileNumber.Checked)
            {
                if (Global.Instance.MandatoryFieldEmpty(this.txtMobileNumber, false, ref message))
                {
                    this.errorProvider1.SetError(this.txtMobileNumber, message);
                    this.txtMobileNumber.Focus();
                    return (false);
                }
            }
            if (chkQueryRegisterTime.Checked)
            {
                var timeSpan = dteEndTime.DateTime - dteStartTime.DateTime;
                if ((timeSpan.TotalSeconds) < 0.0d)
                {
                    return (false);
                }
            }
            if (!this.chkQueryRegisterTime.Checked &&
                !this.chkQueryName.Checked &&
                !this.chkQueryMobileNumber.Checked)
            {
                Global.Instance.InfoDlg("请选择查询条件，至少得有一项");
                return (false);
            }
            return (true);
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void butQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.ValidityCheck()) return;

                Global.Instance.BeginWait();
                QueryOnlineBookingModel queryOnlineBookingModel = new QueryOnlineBookingModel();
                if (chkFuzzyMobileNumber.Checked)
                    queryOnlineBookingModel.isCustomerPhone = true;

                if (chkFuzzyName.Checked)
                    queryOnlineBookingModel.isCustomerName = true;

                queryOnlineBookingModel.isAll = true;
                
                if (handComboBox.SelectedIndex != 0)
                    queryOnlineBookingModel.isAll = false;

                if (handComboBox.SelectedIndex == 1)
                    queryOnlineBookingModel.handle = true;

                if (handComboBox.SelectedIndex == 2)
                    queryOnlineBookingModel.handle = false;

                if (chkQueryRegisterTime.Checked)
                {
                    queryOnlineBookingModel.isTime = true;
                    queryOnlineBookingModel.createDateStart = dteStartTime.DateTime;
                    queryOnlineBookingModel.createDateEnd = dteEndTime.DateTime;
                }

                if (chkQueryMobileNumber.Checked)
                    queryOnlineBookingModel.customerPhone = txtMobileNumber.Text;
                if (chkQueryName.Checked)
                    queryOnlineBookingModel.customerName = txtCustomer.Text;

                this.LoadRecordToGrid(queryOnlineBookingModel);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
            finally
            {
                Global.Instance.EndWait();
            }
        }

        private void LoadRecordToGrid(QueryOnlineBookingModel model)
        {
            this.onlineBookingModel = model;
            using (DataSet ds = OnlineBookingDAL.Instance.GetDataSet(model))
            {
                this.gridControl1.DataSource = ds.Tables[0];
                this.gridView1.BestFitColumns();
            }
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            //try
            //{
            //    int r = this.gridView1.FocusedRowHandle;
            //    if (r < 0)
            //    {
            //        return;
            //    }

            //    string transfer = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexTRANSFER_FLG]));
            //    string followStaffName = Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[this.colIndexFOLLOW_STAFF_NAME]));
            //}
            //catch (Exception ex)
            //{
            //    Global.Instance.ShowExceptionDlg(ex);
            //}
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int r = this.gridView1.FocusedRowHandle;
                if (r < 0) return;

                this.ShowCustomerDetailForm(Convert.ToString(this.gridView1.GetRowCellValue(r, this.gridView1.Columns[0])));
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void ShowCustomerDetailForm(string customerId)
        {
            MainForm main = this.ParentForm as MainForm;
            if (main == null)
            {
                return;
            }

            using (DevExpress.Utils.WaitDialogForm waitdlg = new DevExpress.Utils.WaitDialogForm("正在加载...", "请稍候！"))
            {
                string key = string.Concat("C", customerId);
                OnlineBookingDetailForm dlg = new OnlineBookingDetailForm(customerId) { Tag = key };
                Global.Instance.ShowMdiForm<OnlineBookingDetailForm>(main, dlg, key);
            }
        }
    }
}
